package com.frogs.ucenter.v1.service.platform.qq;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.util.date.DateUtil;
import com.frogs.system.util.UserRegisterTypeEnum;
import com.frogs.ucenter.entity.UserAccountInfo;
import com.frogs.ucenter.openplatform.util.QqHttpsConnectUtil;
import com.frogs.ucenter.v1.service.PlatformOperationService;
import com.frogs.ucenter.v1.service.platform.base.PlatformBaseOperationService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 * QQ业务逻辑处理类
 *
 * @author lijian
 * @version 2014年6月27日 - 下午2:14:35
 */
@Service(ApiConstants.VERSION+"qqOperationService")
public class QqOperationServiceImpl extends PlatformBaseOperationService implements PlatformOperationService {

	private static final Logger log = LoggerFactory.getLogger(QqOperationServiceImpl.class);

	@Override
	public JSONObject getOpenId(JSONObject object) {
		JSONObject result = new JSONObject();
		String code = object.getString(ApiConstants.CODE);
		JSONObject qqResult = QqHttpsConnectUtil.getOpenid(code);
		if (qqResult != null && qqResult.getIntValue(ApiConstants.RESULT_CODE) == ApiConstants.RESULT_SUCCESS) {
			String openId = qqResult.getString(ApiConstants.OPEN_ID);
			String token = qqResult.getString(ApiConstants.ACCESS_TOKEN);
			String refreshToken = qqResult.getString(ApiConstants.REFRESH_TOKEN);
			String expiresIn = qqResult.getString(ApiConstants.EXPIRES_IN);
			Date date = new Date();
			Date expiresDate = DateUtil.getDateAfterMs(date, Long.valueOf(expiresIn));
			result.put(ApiConstants.OPEN_ID, openId);
			result.put(ApiConstants.ACCESS_TOKEN, token);
			result.put(ApiConstants.REFRESH_TOKEN, refreshToken);
			result.put(ApiConstants.EXPIRES_DATE, expiresDate);
			result.put(ApiConstants.RESULT_CODE, ApiConstants.RESULT_SUCCESS);
		} else {
			result.put(ApiConstants.RESULT_CODE, ApiConstants.RESULT_FAIL);
			result.put(ApiConstants.RESULT_DESC, qqResult.getString(ApiConstants.RESULT_DESC) == null ? "接口请求失败" : qqResult.getString(ApiConstants.RESULT_DESC));
		}
		return result;
	}

	@Override
	public JSONObject getAccessTokenByRefreshToken(JSONObject object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JSONObject getNickName(JSONObject object) {
		JSONObject result = new JSONObject();
		String openId = object.getString(ApiConstants.OPEN_ID);
		String token = object.getString(ApiConstants.ACCESS_TOKEN);
		JSONObject qqResult = QqHttpsConnectUtil.getNickname(token, openId);
		if (qqResult != null && qqResult.getIntValue(ApiConstants.RESULT_CODE) == ApiConstants.RESULT_SUCCESS) {
			result.put(ApiConstants.NICK_NAME, qqResult.getString(ApiConstants.NICK_NAME));
			result.put(ApiConstants.RESULT_CODE, ApiConstants.RESULT_SUCCESS);
		} else {
			result.put(ApiConstants.RESULT_CODE, ApiConstants.RESULT_FAIL);
			result.put(ApiConstants.RESULT_DESC, qqResult.getString(ApiConstants.RESULT_DESC) == null ? "接口请求失败" : qqResult.getString(ApiConstants.RESULT_DESC));
		}
		return result;
	}

	@Override
	public JSONObject register(JSONObject object) throws Exception {
		Integer isRegister = object.getInteger(ApiConstants.IS_REGISTER);
		UserAccountInfo userAccountInfo = new UserAccountInfo();
		if (isRegister == UserRegisterTypeEnum.REGISTER_BIND_NEW.getId() || isRegister == UserRegisterTypeEnum.REGISTER_BIND_OPENPLATFOMR.getId()) {
				userAccountInfo = this.otherOpenPlatformRegister(object);
		}else {
			userAccountInfo = this.openPlatformBind(object);
		}
		object.put("userAccountInfo", userAccountInfo);
		return object;
	}

}
